﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

using Com.Ompchina.Lib.DBHelper;
using Model.AuthModel;
using IDAL.AuthModel;
namespace SQLServerDAL.AuthModel
{
    /// <summary>
    /// 通用组件，权限模型，数据层，管理成员登录记录
    /// </summary>
    public class ModuleTypeDAL : DalBase, IModuleType
    {
        public ModuleTypeDAL() 
        {
            Schema = "am.";
            TableName = "ModuleType";
            FieldsList = new string[] { "ModuleTypeID, ModuleTypeName, ModuleTypeDescription" };
        }

        #region IModuleType 成员
        /// <summary>
        /// 新增模块分类
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool InsertModuleType(ModuleTypeInfo info)
        {            
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@ModuleTypeName",SqlDbType.NVarChar,20)
                ,new SqlParameter("@ModuleTypeDescription",SqlDbType.NVarChar,100)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
            };
            int i = 0;
            parm[i++].Value = info.ModuleTypeName;
            parm[i++].Value = info.ModuleTypeDescription;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "AddModuleType", parm);
            return parm[parm.Length - 1].Value.ToString().Equals("80");
        }

        /// <summary>
        /// 修改模块分类
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool UpdateModuleType(ModuleTypeInfo info)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@ModuleTypeID",SqlDbType.Int)
                ,new SqlParameter("@ModuleTypeName",SqlDbType.NVarChar,20)
                ,new SqlParameter("@ModuleTypeDescription",SqlDbType.NVarChar,100)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
            };
            int i = 0;
            parm[i++].Value = info.ModuleTypeID;
            parm[i++].Value = info.ModuleTypeName;
            parm[i++].Value = info.ModuleTypeDescription;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "UpdateModuleType", parm);
            return parm[parm.Length - 1].Value.ToString().Equals("80");
        }

        /// <summary>
        /// 删除模块分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteModuleType(int id) 
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@ModuleTypeID",SqlDbType.Int)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
            };
            int i = 0;
            parm[i++].Value = id;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "DeleteModuleType", parm);
            return parm[parm.Length - 1].Value.ToString().Equals("80");
        }

        /// <summary>
        /// 根据编号获取模块分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ModuleTypeInfo GetModuleTypeByID(int id)
        {
            Sql = string.Format("SELECT TOP 1 {0} FROM {1} WHERE ModuleTypeID = {2}", FieldsList[0], Schema + TableName, id);
            ModuleTypeInfo info = new ModuleTypeInfo();
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, Sql);
            if (dr != null)
            {
                if (dr.Read())
                {
                    int i = 0;
                    info.ModuleTypeID = dr.GetInt32(i++);
                    info.ModuleTypeName = dr.GetString(i++);
                    info.ModuleTypeDescription = dr.GetString(i++);
                }
                dr.Close();
            }
            return info;
        }

        /// <summary>
        /// 获取全部的分类
        /// </summary>
        /// <returns></returns>
        public IList<ModuleTypeInfo> ListModuleType()
        {
            Sql = string.Format("SELECT {0} FROM {1}", FieldsList[0], Schema + TableName);
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, Sql);
            List<ModuleTypeInfo> list = new List<ModuleTypeInfo>();
            if (dr != null)
            {
                while (dr.Read())
                {
                    ModuleTypeInfo info = new ModuleTypeInfo();
                    int i = 0;
                    info.ModuleTypeID = dr.GetInt32(i++);
                    info.ModuleTypeName = dr.GetString(i++);
                    info.ModuleTypeDescription = dr.GetString(i++);
                    list.Add(info);
                }
                dr.Close();
            }
            return list;
        }
        #endregion
    }
}
